<template>
  <view class="login-container">
    <!-- 提示登录的图标 -->
    <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
    <!-- 登录按钮 -->
    <button type="primary" class="btn-login" @click="getUserInfo">一键登录</button>
    <!-- 登录提示 -->
    <view class="tips-text">登录后尽享更多权益</view>
  </view>
</template>

<script>
  import {
    mapMutations,
    mapState
  } from 'vuex'

  export default {
    name: "my-login",
    data() {
      return {
        //定义一个对象detail存储cloudID encryptedData...替代教学中的e.detail
        detail: {
          cloudID: '',
          encryptedData: '',
          errMsg: '',
          iv: '',
          rawData: '',
          signature: ''
        }
      };
    },
    computed: {
      ...mapState('m_user', ['redirectInfo'])
    },
    methods: {
      ...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateRedirectInfo']),
      //用户授权之后,获取用户的基本信息
      getUserInfo(e) {
        // console.log('e--->', e);
        uni.getUserProfile({
          lang: 'zh_CN',
          //必须属性用来触发
          desc: '用来授权登录该小程序!',
          //用户授权成功之后的回调
          success: (res) => {
            // console.log("success-->", res);
            uni.$showMsg('授权成功 !')
            //调用持久化存储用户信息
            this.updateUserInfo(res.userInfo)
            //将res对象存放到detail中
            this.detail.cloudID = res.cloudID
            this.detail.encryptedData = res.encryptedData
            this.detail.errMsg = res.errMsg
            this.detail.iv = res.iv
            this.detail.rawData = res.rawData
            this.detail.signature = res.signature
            // console.log('detail-->', this.detail);
            //调用登陆接口换取永久的token
            this.getToken(this.detail)
          },
          fail: (res) => {
            // console.log("fail-->", res);
            uni.$showMsg('您取消了登录授权 !')
          },
          complete: (res) => {
            // 完成之后根据err.Msg提示允许和拒绝授权
            if (res.errMsg === 'getUserProfile:ok') return uni.$showMsg('授权成功')
            if (res.errMsg === 'getUserProfile:fail auth deny') return uni.$showMsg('您取消了授权')

          },
        })
      },
      //获取token
      async getToken(info) {
        //获取code对应的值
        const [err, res] = await uni.login().catch(err => err)
        // console.log('获取token-->', res);
        if (err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败')

        // console.log('---->', res);
        // console.log('info-->', info);
        //准备参数
        const query = {
          code: res.code,
          encryptedData: info.encryptedData,
          iv: info.iv,
          rawData: info.rawData,
          signature: info.signature
        }

        //这一步已经无开发者权限申请token 只能写个死的来自己测试
        // const {
        //   data: loginResult
        // } = await uni.$http.post('/api/public/v1/users/wxlogin', query)

        //定义一个token假数据
        const loginResult = {
          message: {
            user_id: 12,
            user_email_code: null,
            is_active: null,
            user_sex: '男',
            user_qq: '',
            user_tel: '',
            user_xueli: '本科',
            user_hobby: '',
            user_introduce: null,
            create_time: 1525402223,
            update_time: 1525402223,
            token: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEyLCJpYXQiOjE1MjU0MDIyMjMsImV4cCI6MTUyNTQ4ODYyM30.g-4GtEQNPwT_Xs0Pq7Lrco_9DfHQQsBiOKZerkO-O-o"
          },
          meta: {
            msg: '登录成功',
            status: 200
          }
        }
        console.log('loginResult-->', loginResult);
        //由于不是教学中的开发者 所以无法正式授权登录
        if (loginResult.meta.status !== 200) return uni.$showMsg('登录失败 !')
        // uni.$showMsg('登录成功 !')

        //直接把token保存到vuex中
        this.updateToken(loginResult.message.token)
        this.navigateBack()
      },
      navigateBack() {
        if (this.redirectInfo && this.redirectInfo.openType === 'switchTab') {
          uni.switchTab({
            url: this.redirectInfo.from,
            complete: () => {
              this.updateRedirectInfo(null)
            }
          })
        }
      }
    }
  }
</script>

<style lang="scss">
  .login-container {
    // 登录盒子的样式
    height: 750rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #f8f8f8;
    position: relative;
    overflow: hidden;

    // 绘制登录盒子底部的半椭圆造型
    &::after {
      content: '';
      display: block;
      position: absolute;
      width: 100%;
      height: 40px;
      left: 0;
      bottom: 0;
      background-color: white;
      border-radius: 100%;
      transform: translateY(50%);
    }

    // 登录按钮的样式
    .btn-login {
      width: 90%;
      border-radius: 100px;
      margin: 15px 0;
      background-color: #ff7600;
    }

    // 按钮下方提示消息的样式
    .tips-text {
      font-size: 12px;
      color: gray;
    }
  }
</style>
